package com.otp.controller;

import com.otp.pojo.dto.MessageDto;
import com.otp.pojo.dto.ReplyDto;
import com.otp.pojo.entity.Message;
import com.otp.pojo.entity.Reply;
import com.otp.pojo.entity.User;
import com.otp.pojo.request.message.*;
import com.otp.service.MessageService;
import com.otp.service.UserService;
import com.otp.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/message")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private UserService userService;

    @GetMapping
    public ResponseMessage<List<MessageDto>> list() {
        return ResponseMessage.success(messageService.list()
                .stream()
                .map(message -> {
                    User student = userService.getById(message.getStudentId());
                    return message.convertToDto(student.getRealName());
                })
                .toList());
    }

    @GetMapping("/{id}")
    public ResponseMessage<MessageDto> getById(@PathVariable Long id) {
        Message message = messageService.getById(id);
        User student = userService.getById(message.getStudentId());
        return ResponseMessage.success(message.convertToDto(student.getRealName()));
    }

    @PostMapping
    public ResponseMessage<MessageDto> create(@RequestBody MessageCreateRequest request) {
        Message message = messageService.create(request);
        User student = userService.getById(message.getStudentId());
        return ResponseMessage.success(message.convertToDto(student.getRealName()));
    }

    @PutMapping
    public ResponseMessage<MessageDto> update(@RequestBody MessageUpdateRequest request) {
        Message message = messageService.update(request);
        User student = userService.getById(message.getStudentId());
        return ResponseMessage.success(message.convertToDto(student.getRealName()));
    }

    @DeleteMapping("/{id}")
    public ResponseMessage<?> delete(@PathVariable Long id) {
        messageService.delete(id);
        return ResponseMessage.success(null);
    }

    @GetMapping("/{messageId}/reply")
    public ResponseMessage<List<ReplyDto>> getReply(@PathVariable Long messageId) {
        return ResponseMessage.success(messageService.getReplies(messageId)
                .stream()
                .map(reply -> {
                    User user = userService.getById(reply.getTeacherId());
                    return reply.convertToDto(user.getRealName());
                })
                .toList());
    }

    @PostMapping("/{messageId}/reply")
    public ResponseMessage<ReplyDto> createReply(@PathVariable Long messageId, @RequestBody ReplyCreateRequest request) {
        request.setMessageId(messageId);
        Reply reply = messageService.createReply(request);
        User user = userService.getById(reply.getTeacherId());
        return ResponseMessage.success(reply.convertToDto(user.getRealName()));
    }

    @DeleteMapping("/reply/{replyId}")
    public ResponseMessage<Void> deleteReply(@PathVariable Long replyId) {
        messageService.deleteReply(replyId);
        return ResponseMessage.success(null);
    }

    @GetMapping("/notify/{studentId}")
    public ResponseMessage<List<MessageDto>> getNotification(@PathVariable Long studentId) {
        return ResponseMessage.success(messageService.getNotifications(studentId)
                .stream()
                .map(message -> {
                    User student = userService.getById(message.getStudentId());
                    return message.convertToDto(student.getRealName());
                })
                .toList());
    }

    @PostMapping("/{id}/read")
    public ResponseMessage<MessageDto> markAsRead(@PathVariable Long id) {
        return ResponseMessage.success(messageService.markAsRead(id)
                .convertToDto(userService.getById(messageService.getById(id)
                        .getStudentId())
                        .getRealName()));
    }




}